/**
 * 
 */
package leetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zhongfang
 *
 */
public class FindDuplicateFileinSystem {
    public List<List<String>> findDuplicate(String[] paths) {
        List<List<String>> res=new ArrayList<>();
        HashMap<String, ArrayList<String>> content2files=new HashMap<>(20000);
        for (String string : paths) {
			String[] strings=string.split(" ");
			String path=strings[0];
			for (int i = 1; i < strings.length; i++) {
				String doc=strings[i];
				int index=doc.indexOf('(');
				String fileName=doc.substring(0, index);
				String content=doc.substring(index+1,doc.length());
				if(!content2files.containsKey(content)){
					content2files.put(content, new ArrayList<>());
				}
				content2files.get(content).add("\""+path+"/"+fileName+"\"");
			}
		}
        for (ArrayList<String> list : content2files.values()) {
			if(list.size()>1){//有重复
				ArrayList<String> resFile=new ArrayList<>();
				for (String string : list) {
					resFile.add(string);
				}
				res.add(resFile);
			}
		}
        return res;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FindDuplicateFileinSystem fileinSystem=new FindDuplicateFileinSystem();
		String[] paths={"root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"};
		System.out.println(fileinSystem.findDuplicate(paths));
	}

}
